//
//  YQCycleView.m
//  CycleScrollViewDemo
//
//  Created by lanou3g on 16/4/19.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQCycleView.h"

@interface YQCycleView ()<UIScrollViewDelegate>
/**
 *  scrollView
 */
@property (nonatomic, strong) UIScrollView *imgScrollView;
/**
 *  pageControler
 */
@property (nonatomic, strong) UIPageControl *imgPageCOntrol;
/**
 *  复用的imageView
 */
@property (nonatomic, strong) NSMutableArray *imgViewArray;
/**
 *  存放图片的数组
 */
@property (nonatomic, strong) NSMutableArray *imgArray;
/**
 *  记录当前图片的下标
 */
@property (nonatomic, assign) NSInteger index;
/**
 *  定时器
 */
@property (nonatomic, strong) NSTimer *imgTimer;
/**
 *  点击回掉block
 */
@property (nonatomic, copy) void(^tapBlock)(NSInteger index);
/**
 *  时间间隔
 */
@property (nonatomic, assign) NSInteger interval;
/**
 *  是否为网络图片
 */
@property (nonatomic, assign) BOOL isNetImageName;
@end
@implementation YQCycleView
- (instancetype)initWithFrame:(CGRect)frame interVal:(NSTimeInterval)interval
{
    if (self = [super initWithFrame:frame]) {
        self.imgViewArray = [NSMutableArray new];
        self.imgArray = [NSMutableArray new];
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
        self.imgScrollView.pagingEnabled = YES;
        self.imgScrollView.showsVerticalScrollIndicator = NO;
        self.imgScrollView.showsHorizontalScrollIndicator = NO;
        
        [self addSubview:self.imgScrollView];
        self.interval = interval;
        for (int i = 0; i < 3; i++) {
            UIImageView *imgeView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height)];
            //添加到父试图和数组
            [self.imgScrollView addSubview:imgeView];
            [self.imgViewArray addObject:imgeView];
            self.imgScrollView.delegate = self;
        }
            }
    return self;
}
//
- (void)timerAction
{
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width * 2, 0) animated:YES];
    self.index = (self.index + 1) % self.imgNameArray.count;
//     NSLog(@"timeAction:%ld",self.index);
//   [self layoutImages];
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self layoutImages];
    
}
- (void)setPageController
{
    //设置pageControl
    self.imgPageCOntrol = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 40, self.frame.size.width, 40)];
    [self addSubview:self.imgPageCOntrol];
    self.imgPageCOntrol.currentPageIndicatorTintColor = [UIColor blackColor];
    self.imgPageCOntrol.pageIndicatorTintColor = [UIColor redColor];
    self.imgPageCOntrol.currentPage = 0;
    self.imgPageCOntrol.numberOfPages = self.imgNameArray.count;
    self.imgPageCOntrol.enabled = NO;

}
//- (void)setUrlNameArray:(NSMutableArray *)urlNameArray
//{
//    self.isNetImageName = YES;
//    if (_urlNameArray != urlNameArray) {
//        _urlNameArray = urlNameArray;
//        [self setPageController];
//        for (int i = 0; i < urlNameArray.count; i++) {
//            NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:_urlNameArray[i]]];
//            UIImage *img = [UIImage imageWithData:data];
//            [self.imgArray addObject:img];
//            if (i < 3) {
//                [self.imgViewArray[i] setImage:img];
//            }
//
//        }
//    }
//    self.index = 0;
//    [self layoutImages];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//    [self.urlNameArray[1] setUserInteractionEnabled:YES];
//    [self.urlNameArray[1] addGestureRecognizer:tap];
//    if (self.interval <= 0) {
//        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//    }else {
//        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
//    }
//    
//
//
//}
- (void)setImgNameArray:(NSMutableArray *)imgNameArray
{
    self.isNetImageName = NO;
    if (_imgNameArray != imgNameArray) {
        _imgNameArray = imgNameArray;
        [self setPageController];
        for (int i = 0; i < imgNameArray.count; i ++) {
                UIImage *img = [UIImage imageNamed:imgNameArray[i]];
                [self.imgArray addObject:img];
                if (i < 3) {
                    [self.imgViewArray[i] setImage:img];
            }
            
}
//        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
        self.index = 0;
        [self layoutImages];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self.imgViewArray[1] setUserInteractionEnabled:YES];
        [self.imgViewArray[1] addGestureRecognizer:tap];
        if (self.interval <= 0) {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }else {
            self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        }

    }
}
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}
static CGFloat x = 0;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    x = scrollView.contentOffset.x;
    [self.imgTimer setFireDate:[NSDate distantFuture]];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (x > scrollView.contentOffset.x) {
        [self panToLeft:NO index:self.index];
    }else if (x < scrollView.contentOffset.x) {
        [self panToLeft:YES index:self.index];
    }
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:3]];
}
/**
 *  滑动的方向
 *
 *  @param left  向左滑动还是向右划
 *  @param index 图片下标
 */
- (void)panToLeft:(BOOL)left index:(NSInteger)index
{
    if (!left) {
        self.index = (index - 1 + self.imgNameArray.count) % self.imgNameArray.count;
        
    }else
    {
        self.index = (index + 1) % self.imgNameArray.count;
    }
    //切换图片
    [self layoutImages];
    //回位操作
//    [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}
- (void)layoutImages{
//    if (self.isNetImageName) {
//        for (int i = 0; i < 3; i++) {
//            [self.urlNameArray[i] setImage:self.imgArray[(self.index - 1 + i + self.imgArray.count) % self.urlNameArray.count]];
//        }
//        //回位操作
//        [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width, 0)];
//    }

        for (int i = 0; i < 3; i++) {
            [self.imgViewArray[i] setImage:self.imgArray[(self.index - 1 + i + self.imgArray.count) % self.imgNameArray.count]];
        }
        //回位操作
        [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width, 0)];
    
    
}
- (void)setIndex:(NSInteger)index
{
    if (_index != index) {
        _index = index;
        self.imgPageCOntrol.currentPage = index;
    }
}
- (void)addTapBlock:(void (^)(NSInteger index))block
{
    self.tapBlock = block;
}
@end
